package com.example.android;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.android.bean.AddCar;
import com.example.android.utils.api.BackApi;
import com.example.android.utils.BaseModel;
import com.example.android.utils.FileUpload;
import com.example.android.utils.GlideEngine;
import com.example.android.utils.NetUtil;
import com.google.android.material.snackbar.Snackbar;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class addFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    /**
     * 返回上传完成的图片url
     *
     * @return
     */
    public void photoAndCamera(View view, StringBuilder url) {
        PictureSelector.create(this)
                //设置使用模式
                .openGallery(SelectMimeType.ofImage())
                .setImageEngine(GlideEngine.createGlideEngine())
                //设置相册最多选择数
                .setMaxSelectNum(1)
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    //回调函数
                    public void onResult(ArrayList<LocalMedia> result) {
                        //把数据插入到图片组件中
                        GlideEngine.createGlideEngine().loadGridImage(getContext(), result.get(0).getPath(), (ImageView) view);
                        //上传图片到后端
                        FileUpload.uploadImage(result.get(0).getRealPath(), url);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        AddCar addCar = new AddCar();
        View root = inflater.inflate(R.layout.fragment_add, container, false);
        //设置图片单击事件
        ImageView imageView1 = root.findViewById(R.id.imageView);
        ImageView imageView2 = root.findViewById(R.id.imageView3);
        ImageView imageView3 = root.findViewById(R.id.imageView2);
        ImageView imageView4 = root.findViewById(R.id.imageView4);
        List<StringBuilder> s = new ArrayList<>();
        s.add(new StringBuilder(""));
        s.add(new StringBuilder(""));
        s.add(new StringBuilder(""));
        s.add(new StringBuilder(""));
        imageView1.setOnClickListener(view -> {
            photoAndCamera(view, s.get(0));
        });
        imageView2.setOnClickListener(view -> {
            photoAndCamera(view, s.get(1));
        });
        imageView3.setOnClickListener(view -> {
            photoAndCamera(view, s.get(2));
        });
        imageView4.setOnClickListener(view -> {
            photoAndCamera(view, s.get(3));
        });

        // 取车地址
        EditText carAddress = root.findViewById(R.id.carAddress);
        //车牌
        EditText carpn = root.findViewById(R.id.editTextTextPersonName);
        //型号
        EditText carBarnd = root.findViewById(R.id.carBrand);
        //车辆颜色
        EditText carColor = root.findViewById(R.id.color);
        //确定
        Button confrim = root.findViewById(R.id.button5);
        //取消
        Button rebock = root.findViewById(R.id.button6);
        confrim.setOnClickListener(view -> {
            addCar.setBrand(carBarnd.getText().toString());
            addCar.setPlaterNumber(carpn.getText().toString());
            addCar.setColor(carColor.getText().toString());
            addCar.setImage4(s.get(3).toString());
            addCar.setImage3(s.get(2).toString());
            addCar.setImage2(s.get(1).toString());
            addCar.setImage1(s.get(0).toString());
            addCar.setAddress(carAddress.getText().toString());
            //添加车辆请求
            NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(BackApi.class).addCarInfo(addCar).enqueue(new Callback<BaseModel<String>>() {
                @Override
                public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
                    if (response.body().getCode() == 200) {
                        Snackbar.make(view, "添加成功：", Snackbar.LENGTH_LONG).show();
                        Navigation.findNavController(view).navigateUp();
                    } else {
                        Snackbar.make(view, response.body().getMsg(), Snackbar.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onFailure(Call<BaseModel<String>> call, Throwable t) {
                    Snackbar.make(view, t.getMessage(), Snackbar.LENGTH_LONG).show();
                }
            });
        });
        rebock.setOnClickListener(view -> Navigation.findNavController(view).navigateUp());
        return root;
    }


}